package com.solocredit.finance.ui.steps.fm

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.LayoutInflater
import android.view.View
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.solocredit.finance.R
import com.solocredit.finance.app.MyApp
import com.solocredit.finance.base.adapter.MullaOnItemChildClickListener
import com.solocredit.finance.base.utils.DensityUtils
import com.solocredit.finance.base.utils.ImageUtil
import com.solocredit.finance.base.utils.log.MullaLog
import com.solocredit.finance.databinding.FmMullaStepOcrBinding
import com.solocredit.finance.databinding.ItemMullaPageIndicatorBinding
import com.solocredit.finance.http.ApiService
import com.solocredit.finance.http.ApiService.addCommonParams
import com.solocredit.finance.http.ApiService.addRandomParams
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.mvp.MvpFm
import com.solocredit.finance.ui.steps.act.MullaCameraOcrAct
import com.solocredit.finance.ui.steps.act.MullaStepsAct
import com.solocredit.finance.ui.steps.adapter.MullaStepOcrTypeAdapter
import com.solocredit.finance.ui.steps.impl.MullaUploadPresenterImpl
import com.solocredit.finance.ui.steps.view.IMullaUploadView
import com.solocredit.finance.utils.dialog.PhotoDialog
import com.solocredit.finance.utils.dialog.VerificationSendSuccessDialog
import com.solocredit.finance.utils.event.MullaBehaviorTracker
import com.solocredit.finance.utils.event.MullaEventType
import com.solocredit.finance.utils.imagecovert.ImageConvert
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/31 13:40
 * @Annotation：认证流程 - 身份证、护照、驾驶证
 */
class MullaStepOcrFm(): MvpFm<IMullaUploadView, MullaUploadPresenterImpl>(),IMullaUploadView {
    private var _binding: FmMullaStepOcrBinding? = null
    private val binding get() = _binding ?: throw IllegalStateException(
        "Cannot access binding because it is null. Either the view hasn't been created yet, or it has already been destroyed."
    )
    private var stepItem: JSONObject? = null
    //是否选中删除信息,默认选中
    private var isSelectedDelete = true

    private var typeTitleList: ArrayList<String>? = null
    private val typeImgNormalList = arrayListOf(
        R.mipmap.mulla_step_ocr_idcard_normal,
        R.mipmap.mulla_step_ocr_drving_license_normal,
        R.mipmap.mulla_step_ocr_passport_normal,
        R.mipmap.mulla_step_ocr_election_card_normal
    )
    private val typeImgSelectList = arrayListOf(
        R.mipmap.mulla_step_ocr_idcard_selected,
        R.mipmap.mulla_step_ocr_drving_license_selected,
        R.mipmap.mulla_step_ocr_passport_selected,
        R.mipmap.mulla_step_ocr_election_card_selected
    )

    private var typeAdapter: MullaStepOcrTypeAdapter? = null
    private var selectItem: JSONObject? = null

    //是否是正面
    private var isFront = true
    //正面照
    private var imgFrontUrl = ""
    //背面照
    private var imgBackUrl = ""

    //图片选择
    lateinit var pickMedia: ActivityResultLauncher<PickVisualMediaRequest>

    constructor(stepItem: JSONObject): this(){
        this.stepItem = stepItem
    }
    override fun createPresenter(): MullaUploadPresenterImpl {
        return MullaUploadPresenterImpl(context)
    }

    override fun getMullaRootView(inflater: LayoutInflater): View {
        _binding = FmMullaStepOcrBinding.inflate(inflater)
        return binding.root
    }

    override fun initMullaView() {
        rootViewBinding.containerLayout.setBackgroundColor(Color.parseColor("#00000000"))
        stepItem?.let {
            _binding?.textStepTitle?.text = it.optString("showTitle")
        }
        initMullaRecyclerView()
    }

    override fun initMullaListener() {
        _binding?.ocrDeleteTipsLayout?.setOnClickListener {
            isSelectedDelete = !isSelectedDelete
            showMullaOcrDeleteInformationTips()
        }
        //idcard 正面
        _binding?.idcardLayout?.idCardFrontLayout?.setOnClickListener { onMullaClickListener(it) }
        //idcard 反面
        _binding?.idcardLayout?.idCardBackLayout?.setOnClickListener { onMullaClickListener(it) }
        //驾驶证 正面
        _binding?.drvingLicenseLayout?.drvingLicenseFrontLayout?.setOnClickListener { onMullaClickListener(it) }
        //驾驶证 反面
        _binding?.drvingLicenseLayout?.drvingLicenseBackLayout?.setOnClickListener { onMullaClickListener(it) }
        //护照 正面
        _binding?.passportLayout?.passportLayout?.setOnClickListener { onMullaClickListener(it) }
        //选举卡 正面
        _binding?.electionCardLayout?.electionCardFrontLayout?.setOnClickListener { onMullaClickListener(it) }
        //选举卡 反面
        _binding?.electionCardLayout?.electionCardBackLayout?.setOnClickListener { onMullaClickListener(it) }

        //提交
        _binding?.btnNext?.setOnClickListener {
            if (selectItem == null){
                return@setOnClickListener
            }
            var ocrType = ""
            selectItem?.let {
                ocrType = it.optString("ocrType")
            }
            //如果是选举卡类型，需要按照身份证类型上传
            if (ocrType == "ELECTIONCARD"){
                ocrType = "IDCARD"
            }
            HttpHelper.addMullaEventAction(String.format(MullaEventType.CLICK.MULLA_CLICK_STEP_OCR_XX_NEXT,ocrType),"")

            if (!isSibAllowSubmit()){
                showMullaErrorTipsDialog(getString(R.string.tips_submit_error),ocrType)
                return@setOnClickListener
            }
            val params = JSONObject().addCommonParams().addRandomParams().apply {
//                put("path", ApiService.API_PATH.MULLA_API_OCR_INFO_PATH)
                put("cardType",ocrType)//证件类型（PASSPORT、IDCARD、DRIVING、FACE）
            }
            params.put("cardImgUrl",imgFrontUrl)//证件图片正面
            if (imgBackUrl.isNotEmpty()){
                params.put("cardBackImgUrl",imgBackUrl)//证件图片反面
            }

            // TODO: Step Next 批量埋点上报
            MullaBehaviorTracker.getInstance(MyApp.app).reportImmediately(null)
            if (isSelectedDelete){
                showMullaDeleteInfoDialog(params)
            } else {
                val act = context as MullaStepsAct
                act.onMullaSubmitProcessAction(ApiService.API_PATH.MULLA_API_OCR_INFO_PATH,params)
            }
        }

        // 1-注册图库组件
        pickMedia = registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
            if (uri != null) {
                // 图片处理
                ImageConvert.covert(context = requireContext(), uri = uri) { localPath, bitmap ->
                    if (localPath != null){
                        MullaLog.e(">>>>>>>album>>>>>>>>>>$localPath")
                        showMullaLoading(isTOuchOut = false, isCancel = false)
                        presenter.onMullaUploadImageAction(localPath)
                    }
                }
            } else {
                MullaLog.e("No media selected")
            }
        }
    }

    override fun initMullaData() {
        //避免选择图片后重复埋点
        HttpHelper.addMullaEventAction(String.format(MullaEventType.PAGE.MULLA_PAGE_STEP_OCR_XX_START,stepItem?.optString("stepTitle")),"")
        val showApprovedKey1 = getString(R.string.step_selfie_upload_credit_limit_approved_tips_key1)
        val showApprovedKey2 = getString(R.string.step_selfie_upload_credit_limit_approved_tips_key2)
        val showApprovedText = String.format(getString(R.string.step_selfie_upload_credit_limit_approved_tips),showApprovedKey1,showApprovedKey2)
        val startApprovedIndex1 = showApprovedText.indexOf(showApprovedKey1)
        val startApprovedIndex2 = showApprovedText.indexOf(showApprovedKey2)
        val approvedTipsSpannable = SpannableString(showApprovedText)
        approvedTipsSpannable.setSpan(ForegroundColorSpan(Color.parseColor("#FF00CB48")), startApprovedIndex1, startApprovedIndex1 + showApprovedKey1.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        approvedTipsSpannable.setSpan(ForegroundColorSpan(Color.parseColor("#FF00CB48")), startApprovedIndex2, startApprovedIndex2 + showApprovedKey2.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        //加粗
        approvedTipsSpannable.setSpan(StyleSpan(Typeface.BOLD), startApprovedIndex1, startApprovedIndex1 + showApprovedKey1.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        approvedTipsSpannable.setSpan(StyleSpan(Typeface.BOLD), startApprovedIndex2, startApprovedIndex2 + showApprovedKey2.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        binding.textStepOcrLimitApprovedTips.text = approvedTipsSpannable

        typeTitleList = arrayListOf(
            "${getString(R.string.step_ocr_idcard)},IDCARD",
            "${getString(R.string.step_ocr_drving_license)},DRIVING",
            "${getString(R.string.step_ocr_drving_passport)},PASSPORT",
            "${getString(R.string.step_ocr_election_card)},ELECTIONCARD",
        )
        val typeList = arrayListOf<JSONObject>()
        typeTitleList?.let { list ->
            for (i in 0..< list.size){
                val item = JSONObject()
                item.put("ocrTitle",list[i].split(",")[0])
                item.put("ocrType",list[i].split(",")[1])
                item.put("imgSelect",typeImgSelectList[i])
                item.put("imgNormal",typeImgNormalList[i])
                if (i == 0){
                    item.put("isSelect",true)
                    selectItem = item
                } else {
                    item.put("isSelect",false)
                }
                typeList.add(item)
            }
        }
        typeAdapter?.data = typeList
        //设置显示Ocr类型
        showMullaOcrTypeViews()
    }

    /**
     * 初始化View
     */
    @SuppressLint("NotifyDataSetChanged")
    private fun initMullaRecyclerView(){
//        _binding?.recyclerView?.setHasFixedSize(true)
        _binding?.recyclerView?.isNestedScrollingEnabled = false
        _binding?.recyclerView?.isFocusableInTouchMode = false //不需要焦点
        val layoutManager = LinearLayoutManager(context)
        layoutManager.orientation = RecyclerView.HORIZONTAL
        _binding?.recyclerView?.layoutManager = layoutManager
        val topDecoration = DensityUtils.dp2px(8f)
//        _binding?.recyclerView.addItemDecoration(GridSpacingItemDecoration(3,topDecoration,false))
        typeAdapter = MullaStepOcrTypeAdapter(_binding?.recyclerView)
        _binding?.recyclerView?.adapter = typeAdapter
        _binding?.recyclerView?.itemAnimator = DefaultItemAnimator()
        typeAdapter?.setOnItemChildClickListener(MullaOnItemChildClickListener { _, _, position ->
            typeAdapter?.let {
                val item = it.getItem(position)
                if (item.optBoolean("isSelect", false)) {
                    return@MullaOnItemChildClickListener
                }
                selectItem = item
                for (i in 0..<it.itemCount) {
                    val obj = it.getItem(i)
                    obj.put("isSelect", false)
                    if (i == position) {
                        obj.put("isSelect", true)
                    }
                }
                it.notifyDataSetChanged()
            }
            //设置显示Ocr类型
            showMullaOcrTypeViews()
        })
    }

    /**
     * 添加指示器
     */
    private fun addIndicatorViews(){
        _binding?.indicatorLayout?.removeAllViews()
        typeAdapter?.let {
            for (i in 0..< it.itemCount){
                val item = it.getItem(i)
                val indicatorBinding = ItemMullaPageIndicatorBinding.inflate(layoutInflater)
                when (item.optBoolean("isSelect",false)){
                    true -> {
                        indicatorBinding.imgView.setImageResource(R.drawable.mulla_indicator_select)
                    }
                    else -> {
                        indicatorBinding.imgView.setImageResource(R.drawable.mulla_indicator_normal)
                    }
                }
                _binding?.indicatorLayout?.addView(indicatorBinding.root)
            }
        }
    }

    /**
     * Ocr拍照
     */
    private val takePhotoResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
        when (result.resultCode) {
            Activity.RESULT_OK -> {
                MullaLog.e(">>>>>>onActivityResult: data = " + result.data)
                val imgLocalPath = result.data?.getStringExtra("imgLocalPath").toString()
                val imgUrl = result.data?.getStringExtra("imgUrl").toString()
                showMullaUploadImageSuccess(imgLocalPath,imgUrl)
            }
        }
    }

    /**
     * 显示Ocr类型
     */
    private fun showMullaOcrTypeViews(){
        addIndicatorViews()
        imgFrontUrl = ""
        imgBackUrl = ""
        _binding?.let { binding ->
            binding.idcardLayout.idcardLayout.visibility = View.GONE
            binding.idcardLayout.imgFront.setImageResource(R.mipmap.mulla_step_ocr_front)
            binding.idcardLayout.viewFrontMask.visibility = View.GONE
            binding.idcardLayout.imgFrontUpload.setImageResource(R.mipmap.mulla_step_ocr_upload_icon)
            binding.idcardLayout.textFrontUpload.text = getString(R.string.step_ocr_click_upload)
            binding.idcardLayout.textFrontUpload.setTextColor(Color.parseColor("#FF999999"))
            binding.idcardLayout.imgBack.setImageResource(R.mipmap.mulla_step_ocr_back)
            binding.idcardLayout.viewBackMask.visibility = View.GONE
            binding.idcardLayout.imgBackUpload.setImageResource(R.mipmap.mulla_step_ocr_upload_icon)
            binding.idcardLayout.textBackUpload.text = getString(R.string.step_ocr_click_upload)
            binding.idcardLayout.textBackUpload.setTextColor(Color.parseColor("#FF999999"))

            binding.drvingLicenseLayout.drvingLicenseLayout.visibility = View.GONE
            binding.drvingLicenseLayout.imgFront.setImageResource(R.mipmap.mulla_step_ocr_front)
            binding.drvingLicenseLayout.viewFrontMask.visibility = View.GONE
            binding.drvingLicenseLayout.imgFrontUpload.setImageResource(R.mipmap.mulla_step_ocr_upload_icon)
            binding.drvingLicenseLayout.textFrontUpload.text = getString(R.string.step_ocr_click_upload)
            binding.drvingLicenseLayout.textFrontUpload.setTextColor(Color.parseColor("#FF999999"))
            binding.drvingLicenseLayout.imgBack.setImageResource(R.mipmap.mulla_step_ocr_back)
            binding.drvingLicenseLayout.viewBackMask.visibility = View.GONE
            binding.drvingLicenseLayout.imgBackUpload.setImageResource(R.mipmap.mulla_step_ocr_upload_icon)
            binding.drvingLicenseLayout.textBackUpload.text = getString(R.string.step_ocr_click_upload)
            binding.drvingLicenseLayout.textBackUpload.setTextColor(Color.parseColor("#FF999999"))

            binding.passportLayout.passportLayout.visibility = View.GONE
            binding.passportLayout.imgView.setImageResource(R.mipmap.mulla_step_ocr_front)
            binding.passportLayout.viewMask.visibility = View.GONE
            binding.passportLayout.imgUpload.setImageResource(R.mipmap.mulla_step_ocr_upload_icon)
            binding.passportLayout.textUpload.text = getString(R.string.step_ocr_click_upload)
            binding.passportLayout.textUpload.setTextColor(Color.parseColor("#FF999999"))

            binding.electionCardLayout.electionCardLayout.visibility = View.GONE
            binding.electionCardLayout.imgFront.setImageResource(R.mipmap.mulla_step_ocr_front)
            binding.electionCardLayout.viewFrontMask.visibility = View.GONE
            binding.electionCardLayout.imgFrontUpload.setImageResource(R.mipmap.mulla_step_ocr_upload_icon)
            binding.electionCardLayout.textFrontUpload.text = getString(R.string.step_ocr_click_upload)
            binding.electionCardLayout.textFrontUpload.setTextColor(Color.parseColor("#FF999999"))
            binding.electionCardLayout.imgBack.setImageResource(R.mipmap.mulla_step_ocr_back)
            binding.electionCardLayout.viewBackMask.visibility = View.GONE
            binding.electionCardLayout.imgBackUpload.setImageResource(R.mipmap.mulla_step_ocr_upload_icon)
            binding.electionCardLayout.textBackUpload.text = getString(R.string.step_ocr_click_upload)
            binding.electionCardLayout.textBackUpload.setTextColor(Color.parseColor("#FF999999"))

            when(selectItem?.optString("ocrType")){
                "DRIVING" -> {//驾驶证
                    binding.drvingLicenseLayout.drvingLicenseLayout.visibility = View.VISIBLE
                }
                "PASSPORT" -> {//护照
                    binding.passportLayout.passportLayout.visibility = View.VISIBLE
                }
                "ELECTIONCARD" -> {//选举卡
                    binding.electionCardLayout.electionCardLayout.visibility = View.VISIBLE
                }
                else -> {//身份证
                    binding.idcardLayout.idcardLayout.visibility = View.VISIBLE
                }
            }
        }
    }

    /**
     * 点击选择图片
     */
    private fun onMullaClickListener(v: View){
        when(v.id){
            _binding?.idcardLayout?.idCardFrontLayout?.id -> {//身份证 正面
                isFront = true
                showMullaPhotoDialog("IdCard Front")
            }
            _binding?.idcardLayout?.idCardBackLayout?.id -> {//身份证 反面
                isFront = false
                showMullaPhotoDialog("IdCard Back")
            }
            _binding?.drvingLicenseLayout?.drvingLicenseFrontLayout?.id -> {//驾驶证 正面
                isFront = true
                showMullaPhotoDialog("DriverLicense Front")
            }
            _binding?.drvingLicenseLayout?.drvingLicenseBackLayout?.id -> {//驾驶证 反面
                isFront = false
                showMullaPhotoDialog("DriverLicense Back")
            }
            _binding?.passportLayout?.passportLayout?.id -> {//护照
                showMullaPhotoDialog("Passport")
            }
            _binding?.electionCardLayout?.electionCardFrontLayout?.id -> {//选举卡 正面
                isFront = true
                showMullaPhotoDialog("ElectionCard Front")
            }
            _binding?.electionCardLayout?.electionCardBackLayout?.id -> {//选举卡 反面
                isFront = false
                showMullaPhotoDialog("ElectionCard Back")
            }
        }
    }

    /**
     * 相机、相册
     */
    private fun showMullaPhotoDialog(eventRemark: String){
        val photoDialog = PhotoDialog(context,eventRemark,object : PhotoDialog.IDialogCallback{
            override fun openCamera() {
                HttpHelper.addMullaEventAction(MullaEventType.CLICK.MULLA_CLICK_CAMERA,eventRemark)
                openSibCamera()
            }
            override fun openAlbum() {
                HttpHelper.addMullaEventAction(MullaEventType.CLICK.MULLA_CLICK_ALBUM,eventRemark)
                openSibAlbum()
            }
        })
        photoDialog.setCancelable(true)
        photoDialog.setCanceledOnTouchOutside(true)
        photoDialog.show()
    }

    /**
     * 打开相册
     */
    private fun openSibAlbum(){
        pickMedia.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
    }

    /**
     * 打开相机
     */
    private fun openSibCamera(){
        if (selectItem != null){
            takePhotoResultLauncher.launch(Intent(context, MullaCameraOcrAct::class.java))
        }
    }

    /**
     * 图片上传成功
     */
    override fun showMullaUploadImageSuccess(imgPath: String, imgUrl: String) {
        if (selectItem == null){
            return
        }
        _binding?.let { binding ->
            when(selectItem?.optString("ocrType")){
                "DRIVING" -> {//驾驶证
                    if (isFront){
                        imgFrontUrl = imgUrl
                        ImageUtil.instance?.loadImageUrl(requireContext(),binding.drvingLicenseLayout.imgFront,imgPath)
                        binding.drvingLicenseLayout.viewFrontMask.visibility = View.VISIBLE
                        binding.drvingLicenseLayout.imgFrontUpload.setImageResource(R.mipmap.mulla_step_ocr_re_upload_icon)
                        binding.drvingLicenseLayout.textFrontUpload.text = getString(R.string.step_ocr_re_upload)
                        binding.drvingLicenseLayout.textFrontUpload.setTextColor(Color.parseColor("#FFFFFFFF"))
                    } else {
                        imgBackUrl = imgUrl
                        ImageUtil.instance?.loadImageUrl(requireContext(),binding.drvingLicenseLayout.imgBack,imgPath)
                        binding.drvingLicenseLayout.viewBackMask.visibility = View.VISIBLE
                        binding.drvingLicenseLayout.imgBackUpload.setImageResource(R.mipmap.mulla_step_ocr_re_upload_icon)
                        binding.drvingLicenseLayout.textBackUpload.text = getString(R.string.step_ocr_re_upload)
                        binding.drvingLicenseLayout.textBackUpload.setTextColor(Color.parseColor("#FFFFFFFF"))
                    }
                }
                "PASSPORT" -> {//护照
                    imgFrontUrl = imgUrl
                    ImageUtil.instance?.loadImageUrl(requireContext(),binding.passportLayout.imgView,imgPath)
                    binding.passportLayout.viewMask.visibility = View.VISIBLE
                    binding.passportLayout.imgUpload.setImageResource(R.mipmap.mulla_step_ocr_re_upload_icon)
                    binding.passportLayout.textUpload.text = getString(R.string.step_ocr_re_upload)
                    binding.passportLayout.textUpload.setTextColor(Color.parseColor("#FFFFFFFF"))
                }
                "ELECTIONCARD" -> {//选举卡
                    if (isFront){
                        imgFrontUrl = imgUrl
                        ImageUtil.instance?.loadImageUrl(requireContext(),binding.electionCardLayout.imgFront,imgPath)
                        binding.electionCardLayout.viewFrontMask.visibility = View.VISIBLE
                        binding.electionCardLayout.imgFrontUpload.setImageResource(R.mipmap.mulla_step_ocr_re_upload_icon)
                        binding.electionCardLayout.textFrontUpload.text = getString(R.string.step_ocr_re_upload)
                        binding.electionCardLayout.textFrontUpload.setTextColor(Color.parseColor("#FFFFFFFF"))
                    } else {
                        imgBackUrl = imgUrl
                        ImageUtil.instance?.loadImageUrl(requireContext(),binding.electionCardLayout.imgBack,imgPath)
                        binding.electionCardLayout.viewBackMask.visibility = View.VISIBLE
                        binding.electionCardLayout.imgBackUpload.setImageResource(R.mipmap.mulla_step_ocr_re_upload_icon)
                        binding.electionCardLayout.textBackUpload.text = getString(R.string.step_ocr_re_upload)
                        binding.electionCardLayout.textBackUpload.setTextColor(Color.parseColor("#FFFFFFFF"))
                    }
                }
                else -> {//身份证
                    if (isFront){
                        imgFrontUrl = imgUrl
                        ImageUtil.instance?.loadImageUrl(requireContext(),binding.idcardLayout.imgFront,imgPath)
                        binding.idcardLayout.viewFrontMask.visibility = View.VISIBLE
                        binding.idcardLayout.imgFrontUpload.setImageResource(R.mipmap.mulla_step_ocr_re_upload_icon)
                        binding.idcardLayout.textFrontUpload.text = getString(R.string.step_ocr_re_upload)
                        binding.idcardLayout.textFrontUpload.setTextColor(Color.parseColor("#FFFFFFFF"))
                    } else {
                        imgBackUrl = imgUrl
                        ImageUtil.instance?.loadImageUrl(requireContext(),binding.idcardLayout.imgBack,imgPath)
                        binding.idcardLayout.viewBackMask.visibility = View.VISIBLE
                        binding.idcardLayout.imgBackUpload.setImageResource(R.mipmap.mulla_step_ocr_re_upload_icon)
                        binding.idcardLayout.textBackUpload.text = getString(R.string.step_ocr_re_upload)
                        binding.idcardLayout.textBackUpload.setTextColor(Color.parseColor("#FFFFFFFF"))
                    }
                }
            }
        }
    }

    private fun showMullaOcrDeleteInformationTips(){
        when(isSelectedDelete){
            true -> {
                _binding?.imgOcrDeleteTipsSelect?.setImageResource(R.mipmap.mulla_coupon_selected)
            }
            else -> {
                _binding?.imgOcrDeleteTipsSelect?.setImageResource(R.mipmap.mulla_coupon_normal)
            }
        }
    }


    /**
     * 检查是否填写完整
     * 当且仅当 isMust=”NO“为非必填
     */
    private fun isSibAllowSubmit(): Boolean{
        if (imgFrontUrl.isEmpty() || selectItem == null){
            return false
        }
        selectItem?.let {
            val ocrType = it.optString("ocrType")
            if (imgBackUrl.isEmpty() && (ocrType == "DRIVING" || ocrType == "IDCARD" || ocrType == "ELECTIONCARD")){
                return false
            }
        }
        return true
    }

    private fun showMullaDeleteInfoDialog(params: JSONObject){
        val sendSuccessDialog = VerificationSendSuccessDialog(requireContext())
        sendSuccessDialog.setCancelable(true)
        sendSuccessDialog.setCanceledOnTouchOutside(false)
        sendSuccessDialog.show()
        sendSuccessDialog.showMullaSuccessTips(getString(R.string.step_selfie_delete_submit_success_tips))
        launch(Dispatchers.Main){
            delay(1500)
            if (isAdded && !requireActivity().isFinishing && sendSuccessDialog.isShowing) {
                sendSuccessDialog.dismiss()
            }

            val act = context as MullaStepsAct
            act.onMullaSubmitProcessAction(ApiService.API_PATH.MULLA_API_OCR_INFO_PATH,params)
        }
    }

    override fun onStart() {
        super.onStart()
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addMullaEventAction(String.format(MullaEventType.PAGE.MULLA_PAGE_STEP_OCR_XX_END,stepItem?.optString("stepTitle")),"")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}